package Testdemo1;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:周次煜
 * Date: 2024-04-23
 * Time：15:32
 */


class Stu {
    String name;
    int age;

    public static void main(String[] args) {
        PriorityQueue<Stu> priorityQueue=new PriorityQueue<>(new Comparator<Stu>() {
            @Override
            public int compare(Stu o1, Stu o2) {


                // 默认小跟堆
                return o1.age-o2.age;
            }
        });



        Map<String,Integer> map=new HashMap<>();
        Set<Map.Entry<String,Integer>> set=map.entrySet();

    }
}
public class Test3 {

    public static List<String> topKFrequent(String[] words, int k) {
        List<String> list=new LinkedList<>();
        Map<String,Integer> map=new HashMap<>();
        // 统计单词个数
        for (int i = 0; i < words.length; i++) {
            String word=words[i];
            if (map.containsKey(word)) {
                int count=map.get(word);
                map.put(word,count+1);
            } else {
                map.put(word,1);
            }
        }


        PriorityQueue<Map.Entry<String, Integer>> minHeap=new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {

            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {

                if (o1.getValue().compareTo(o2.getValue())==0) {
                    return o2.getKey().compareTo(o1.getKey());
                }

                return o1.getValue().compareTo(o2.getValue());
            }
        });



        // 建立小根堆
        for (Map.Entry<String,Integer> t: map.entrySet()) {
            if (minHeap.size() < k) {
                minHeap.add(t);
            } else  {
                int minInt=minHeap.peek().getValue();
               String  minS= minHeap.peek().getKey();
                if (t.getValue() > minInt) {
                    minHeap.poll();
                    minHeap.add(t);
                } else if (t.getValue() ==minInt && t.getKey().compareTo(minS) < 0) {
                    minHeap.poll();
                    minHeap.add(t);
                }
            }
        }


        // 出堆顶元素
        while (!minHeap.isEmpty()) {
            list.add(minHeap.poll().getKey());
        }


        // 反转顺序表
        Collections.reverse(list);

        return  list;
    }
    public static void main1(String[] args) {
        String []s={"i", "love", "leetcode", "i", "love", "coding"};
        System.out.println(topKFrequent(s,2));
    }

    public static void main(String[] args) {
        Map<String,Integer> map=new HashMap<>();
        map.put("张三",19);
        map.put("李四",89);
        map.put("王五",189);
        map.put("黑豹",1234);
        Set<String> entry=map.keySet();
        Collection<Integer> s=map.values();
//        System.out.println(s);
//        System.out.println(entry);


        System.out.println("==== Collection<Integer> 的 values 值 ===== ");
        for (Integer i:  s) {
            System.out.println(s);
        }

        System.out.println("========= Set<String>  的key 值 ====");
        for (String str:entry) {
            System.out.println(str);
        }


        Set<Map.Entry<String,Integer>> set=map.entrySet();
        System.out.println("==========Set<Map.Entry<String,Integer>> 的 value 和 key 的值");
        for (Map.Entry<String,Integer> entry1 : set) {
            System.out.println("key为："+entry1.getKey()+"val为："+entry1.getValue());
        }

        System.out.println("========== Iterator<Map.Entry<String,Integer>> 的 value 和 key 的值");
        Iterator<Map.Entry<String,Integer>> iterator=set.iterator();
        while ( iterator.hasNext()) {
            System.out.println("key为："+iterator.next().getKey()+"val为："+iterator.next().getValue());

        }
    }

}
